require 'mongrel_cluster/recipes'
require 'erb'

set :application, "otwbingo"
set :shared_server, "bingo.transformativeworks.org"
#set :shared_dir, "/home/nnovik/#{application}"

set :repository,  "http://#{application}.googlecode.com/svn/trunk/"

# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, "/var/www/#{application}"
set :mongrel_conf, "#{deploy_to}/current/config/mongrel_cluster.yml"
set :mongrel_rails, "/usr/local/bin/mongrel_rails"

# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion

set :use_sudo, false

role :app, shared_server
role :web, shared_server
role :db,  shared_server, :primary => true

## ADDITIONAL TASKS

before "deploy:setup", :db
after "deploy:update_code", "db:symlink"

namespace :db do
  desc "Create database.yml in shared/config" 
  task :default do
    set(:mysqluser) do
      Capistrano::CLI.ui.ask "MySQL username: "
    end

    set(:mysqlpassword) do
      Capistrano::CLI.ui.ask "MySQL password: "
    end

    database_configuration = ERB.new <<-EOF
    login: &login
      adapter: mysql
      host: localhost
      encoding: utf8
      socket: /var/run/mysqld/mysqld.sock
      username: #{mysqluser}
      password: #{mysqlpassword}

    development:
      database: <%= "#{application}_development" %>
      <<: *login

    test:
      database: <%= "#{application}_test" %>
      <<: *login

    production:
      database: <%= "#{application}_production" %>
      <<: *login
    EOF

    run "mkdir -p #{shared_path}/config" 
    put database_configuration.result, "#{shared_path}/config/database.yml" 
  end

  desc "Link in the production database.yml" 
  task :symlink do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" 
  end
end

